package com.app.buss.ui.dashboard;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.buss.R;
import com.app.buss.adapter.TopicAdapter;
import com.app.buss.bean.TopicBean;
import com.app.buss.ui.AddTopicActivity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

import static android.content.Context.MODE_PRIVATE;

public class DashboardFragment extends Fragment {


    @BindView(R.id.toolbar_img)
    ImageView toolbarImg;
    @BindView(R.id.rv_topics)
    RecyclerView rvTopics;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    private TopicAdapter adapter;
    private List<TopicBean> list = new ArrayList<>();

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        ButterKnife.bind(this,root);
        toolbarTitle.setText("Topics");
        toolbarImg.setVisibility(View.VISIBLE);
        toolbarImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), AddTopicActivity.class));
            }
        });
        adapter = new TopicAdapter(list,getActivity());
        rvTopics.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvTopics.setAdapter(adapter);
        initTopic();
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        initLocalTopic();
    }

    private void initLocalTopic() {
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("topic",MODE_PRIVATE);
        int size = sharedPreferences.getInt("size",0);
        if(size>0){
            for(int i=0;i<size;i++){
                String content = sharedPreferences.getString(i+1+"","");
                list.add(new TopicBean("Hero123",content,"today",0,0));
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void initTopic() {
        list.add(new TopicBean("ZoroHz","Who knows what's going on in the stock market?","3 days ago",10,2));
        list.add(new TopicBean("Hero11","This year's market is really bad","5 days ago",2,0));
        list.add(new TopicBean("Hero24","Who can tell me, how can I buy it?","7 days ago",5,1));
        list.add(new TopicBean("Liza Picasso","It's all fuckin 'idiots, hahahahahahahaaahhaahahahahhahaha","8 days ago",20,2));
        list.add(new TopicBean("Wliian Z","Who can tell me what this platform is for?","10 days ago",3,1));
        adapter.notifyDataSetChanged();
    }


}
